/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package movietracker;
//imports
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author CaronJB
 */
public class Database
{
    Connection conn;
    //constructor to create a connection to the database
    public Database()
    {
        try{
            //get the driver for
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        }//end try
        catch(Exception ex){
            //catch to handle any errors
            System.out.println("Exception!!!!!" + ex.getMessage());
            System.exit(0);
        }//end catch
    }//end constructor

    public void dataBaseConnect()
    {
        try{
            conn = DriverManager.getConnection("jdbc:mysql://localhost",
                    "root", "merrimack");
        }//end try
        catch(SQLException ex){
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        }//end catch
    }//end of dataBaseConnect Method

    public void databaseDisconn()
    {
        try{
            conn.close();
        }//end try
        catch(Exception e){
            System.out.println("What Happened Here??");
        }//end catch
    }//end database disconnect

    public boolean executeStatement(String sql)
    {
         //if to see if connected
        if(conn == null)
        {
            System.out.println("Not Connected to Database");
            return false;
        }//end if

        try{
           Statement statement = conn.createStatement();
           String useDB = "use movieApp;";
           statement.execute(useDB);
           statement.execute(sql);
           return true;
        }//end try
        catch(SQLException ex){
            System.out.println("Your SQL code did not execue right");
            return false;
        }//end catch
    }//end bool createStatement

        public ResultSet getResult(String getResultSet)
    {
        ResultSet rs = null;
        //if to check the connection
        if(conn == null)
        {
            System.out.println("Not connected to database");
            return null;
        }//end if satement

        try{
            Statement statement = conn.createStatement();
            String useDB = "use movieApp;";
            statement.execute(useDB);
            rs = statement.executeQuery(getResultSet);

        }//end try
        catch(SQLException e)
        {
            System.out.println("something is wrong");
        }//end catch
        return rs;
    }//end getResult
}//end class
